<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('shop.market.coupon.receive.quick Search Fields') })"
        >
            <el-popconfirm
                @confirm="onRestoreAction"
                :confirm-button-text="t('shop.market.coupon.receive.restore')"
                :cancel-button-text="t('Cancel')"
                confirmButtonType="success"
                :title="t('shop.market.coupon.receive.Are you sure to restore the selected record?')"
                width="200"
                :disabled="baTable.table.selection!.length > 0 ? false : true"
            >
                <template #reference>
                    <div class="mlr-12">
                        <el-tooltip :content="t('shop.market.coupon.receive.Restore the selected record to user')" placement="top">
                            <el-button
                                v-auth="'restore'"
                                v-blur
                                :disabled="baTable.table.selection!.length > 0 ? false : true"
                                class="table-header-operate"
                                type="success"
                            >
                                <Icon color="#ffffff" name="el-icon-RefreshRight" />
                                <span class="table-header-operate-text">{{ t('shop.market.coupon.receive.restore') }}</span>
                            </el-button>
                        </el-tooltip>
                    </div>
                </template>
            </el-popconfirm>
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">
            <template #discount_desc>
                <el-table-column label="折扣描述" align="center" min-width="230" show-overflow-tooltip>
                    <template #default="{ row }">
                        <div class="discount-desc">
                            <div v-if="row.coupon.discount_type == 1">
                                满 <el-tag>{{ row.coupon.min_price }}</el-tag> 元， 减 <el-tag type="danger">{{ row.coupon.discount_amount }}</el-tag>
                                元
                            </div>
                            <div v-if="row.coupon.discount_type == 2">
                                满 <el-tag>{{ row.coupon.min_price }}</el-tag> 元， 打 <el-tag type="success">{{ row.coupon.discount_ratio }}</el-tag>
                                折
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #coupon_time>
                <el-table-column label="优惠券有效期" width="230" align="center">
                    <template #default="{ row }">
                        <div style="display: flex; flex-direction: column">
                            {{ $t('shop.market.coupon.receive.start_time') }}：{{ row.start_time }}
                            <div style="display: flex; flex-direction: column">
                                {{ $t('shop.market.coupon.receive.end_time') }}：{{ row.end_time }}
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import { restore } from '/@/api/backend/shop/market/coupon'
import { auth } from '/@/utils/common'

defineOptions({
    name: 'shop/market/coupon/receive',
})

const { t } = useI18n()
const tableRef = ref()

let optButtons: OptButton[] = [
    {
        render: 'confirmButton',
        name: 'delete',
        title: 'Delete',
        text: '',
        type: 'danger',
        icon: 'fa fa-trash',
        class: 'table-row-delete',
        popconfirm: {
            confirmButtonText: t('Delete'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            title: t('Are you sure to delete the selected record?'),
        },
        disabledTip: false,
        display(row) {
            return row.status == 1 && row.delete_time == 0
        },
    },
    {
        render: 'confirmButton',
        name: 'recovery',
        title: 'shop.market.coupon.receive.restore',
        text: '',
        type: 'success',
        icon: 'el-icon-RefreshRight',
        class: 'table-row-edit',
        popconfirm: {
            confirmButtonText: t('Confirm'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'success',
            title: t('shop.market.coupon.receive.Are you sure to restore the selected record?'),
        },
        disabledTip: false,
        display(row) {
            return auth('restore') && row.delete_time > 0
        },
        click(row) {
            onRestore([row.id])
        },
    },
]

const onRestore = (ids: string[]) => {
    restore({ id: ids }).then(() => {
        baTable.onTableHeaderAction('refresh', {})
    })
}

const onRestoreAction = () => {
    onRestore(baTable.getSelectionIds())
}

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/shop.market.coupon.Receive/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('shop.market.coupon.receive.id'), prop: 'id', align: 'center', width: 70, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('shop.market.coupon.receive.name'),
                prop: 'coupon.name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
                minWidth: 150,
                showOverflowTooltip: true,
            },
            {
                label: t('shop.market.coupon.receive.user_nickname'),
                prop: 'user.nickname',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: false,
                sortable: false,
                minWidth: 150,
                showOverflowTooltip: true,
            },
            {
                label: t('shop.market.coupon.receive.discount_type'),
                prop: 'coupon.discount_type',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: { '1': t('shop.market.coupon.receive.discount_type 1'), '2': t('shop.market.coupon.receive.discount_type 2') },
                width: 120,
                custom: {
                    2: 'success',
                },
            },
            {
                render: 'slot',
                slotName: 'discount_desc',
                operator: false,
            },
            {
                label: t('shop.market.coupon.receive.range_type'),
                prop: 'coupon.range_type',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: { '1': t('shop.market.coupon.receive.range_type 1'), '2': t('shop.market.coupon.receive.range_type 2') },
                width: 120,
                custom: {
                    1: 'success',
                    2: 'warning',
                },
            },
            {
                render: 'slot',
                slotName: 'coupon_time',
                operator: false,
            },
            {
                label: t('shop.market.coupon.receive.status'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: {
                    '1': t('shop.market.coupon.receive.status 1'),
                    '2': t('shop.market.coupon.receive.status 2'),
                    '3': t('shop.market.coupon.receive.status 3'),
                    '4': t('shop.market.coupon.receive.status 4'),
                },
                custom: {
                    '2': 'success',
                    '3': 'danger',
                    '4': 'danger',
                },
                width: 120,
            },
            {
                label: t('shop.market.coupon.receive.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('shop.market.coupon.receive.delete_time'),
                prop: 'delete_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false, fixed: 'right' },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: {
            discount_type: '1',
            discount_amount: 0,
            discount_ratio: 0,
            min_price: 0,
            range_type: 1,
            range_goods_ids: null,
            status: '1',
            start_time: 0,
            end_time: 0,
            recovery_time: 0,
        },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">
:deep(.cell) {
    // 最多两行，多余的显示省略
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    word-wrap: break-word;
    word-break: break-all;
}

.table-header-operate {
    margin-left: 12px;
}
</style>
